package com.yuan.base.widget;

import android.annotation.SuppressLint;
import android.content.Context;
import android.os.Handler;
import android.os.Message;
import android.support.v4.widget.SwipeRefreshLayout;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AbsListView;
import android.widget.FrameLayout;
import android.widget.TextView;

import com.yuan.base.R;
import com.yuan.base.widget.jazzy.JazzyHelper;
import com.yuan.base.widget.jumping.JumpingBeans;
import com.yuan.base.widget.observablescrollview.ObservableListView;


/**
 */
public class AutoLoadList extends FrameLayout {

	public static final int OPT_INIT = 0; // 初始化
	public static final int OPT_REFREASH = 1;// 刷新
	public static final int OPT_LOADMORE = 2;// 加载更多
	private int rowCount = 20;// 每次加载条数

	// --------------------------状态--------------------------
	private final int STATUS_NORMAL = 0;
	private final int STATUS_ON_REFRESH = 1;
	private final int STATUS_ON_LOAD_MORE = 2;
	private int status = STATUS_NORMAL;

	// --------------------------控件--------------------------
	private LayoutInflater inflater;
	private SwipeRefreshLayout base_swipe_refresh_widget;// 下拉刷新
	private ObservableListView base_auto_load_data_list;// 列表
	private TextView tv_base_auto_load_no_data;// 空数据时的显示
	// --------------------------footer--------------------------
	private boolean isNeedShowFooter = true;
	private boolean isOpenLoadMore = true;
	private View footerLayout;
	private JumpingBeans jumpingBeans;
	private TextView tv_load_more;
	private boolean allowLoadMore = true;
	// --------------------------item动画--------------------------
	private final JazzyHelper mHelper;//item动画类

	public AutoLoadList(Context context) {
		super(context);
		mHelper = new JazzyHelper(context, null);
		init(context);
	}

	public AutoLoadList(Context context, AttributeSet attrs) {
		super(context, attrs);
		mHelper = new JazzyHelper(context, attrs);
		init(context);
	}

	public AutoLoadList(Context context, AttributeSet attrs, int defStyleAttr) {
		super(context, attrs, defStyleAttr);
		mHelper = new JazzyHelper(context, attrs);
		init(context);
	}

	private void init(Context context) {
		inflater = LayoutInflater.from(context);
		View view = inflater.inflate(R.layout.base_auto_load_list, this);
		base_swipe_refresh_widget = view.findViewById(R.id.base_swipe_refresh_widget);
		base_auto_load_data_list = view.findViewById(R.id.base_auto_load_data_list);
		tv_base_auto_load_no_data = view.findViewById(R.id.tv_base_auto_load_no_data);

		base_swipe_refresh_widget.setColorSchemeResources(R.color.common_bg);
		// 下拉刷新监听
		base_swipe_refresh_widget.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
			@Override
			public void onRefresh() {
				if (onRefreshingListener != null && status == STATUS_NORMAL) {
					status = STATUS_ON_REFRESH;
					refreshHandler.sendEmptyMessage(1);
				}
			}
		});
		base_auto_load_data_list.setOnScrollListener(mHelper);
		// 设置自动加载更多监听
		mHelper.setOnScrollListener(new AbsListView.OnScrollListener() {
			@Override
			public void onScrollStateChanged(AbsListView view, int scrollState) {
				if (scrollState == SCROLL_STATE_IDLE) {
					if (view.getLastVisiblePosition() == (view.getCount() - 1))
						doLoadMore();
				}
			}

			@Override
			public void onScroll(AbsListView view, int firstVisibleItem, int visibleItemCount, int totalItemCount) {
			}
		});
//		mHelper.setTransitionEffect(new GrowEffect());//item显示的默认动画
	}

	public void setNeedShowFooter(boolean needShowFooter) {
		isNeedShowFooter = needShowFooter;
	}

	public void removeTransitionEffect() {
		mHelper.setTransitionEffect(null);
	}

	public JazzyHelper getmHelper() {
		return mHelper;
	}

	public boolean isNormal() {
		return status == STATUS_NORMAL;
	}

	public boolean isRefreshing() {
		return status == STATUS_ON_REFRESH;
	}

	public boolean isLoading() {
		return status == STATUS_ON_LOAD_MORE;
	}

	// 不要分割线
	public void setNoDivider() {
		base_auto_load_data_list.setDividerHeight(0);
		base_auto_load_data_list.setDivider(null);
	}


	/**
	 * 允许加载更多数据
	 */
	public void showMoreData() {
		if (allowLoadMore) {
			allowLoadMore = true;
			changeLoadMoreStatus(0);
		}
	}

	/**
	 * 没有更多数据
	 */
	public void noMoreData() {
		if (allowLoadMore) {
			allowLoadMore = false;
			changeLoadMoreStatus(2);
		}
	}

	/**
	 * 开始执行下拉刷新
	 */
	public void startRefreshing() {
		if (status == STATUS_NORMAL) {
			status = STATUS_ON_REFRESH;
			base_swipe_refresh_widget.post(new Runnable() {
				@Override
				public void run() {
					base_swipe_refresh_widget.setRefreshing(true);
				}
			});
			if (onRefreshingListener != null) {
				onRefreshingListener.onRefresh();
			}
		}
	}

	/**
	 * 完成刷新和加载更多
	 */
	public void completeRefresh() {
		// 当前显示的数据条数
		int count = 0;
		if (base_auto_load_data_list.getAdapter() != null) {
			count = base_auto_load_data_list.getAdapter().getCount();
		}
		count -= base_auto_load_data_list.getFooterViewsCount();
		count -= base_auto_load_data_list.getHeaderViewsCount();

		if (count == 0) {
			tv_base_auto_load_no_data.setVisibility(VISIBLE);
			allowLoadMore = false;
			if (footerLayout != null) {
				base_auto_load_data_list.removeFooterView(footerLayout);
				footerLayout = null;
			}
		} else {
			tv_base_auto_load_no_data.setVisibility(GONE);
			if (isOpenLoadMore) {
				allowLoadMore = true;
				addFooterLayout();
			}
		}

		if (count > 0 && count % rowCount > 0) {
			noMoreData();
		} else {
			// 下拉刷新时需要重置加载更多
			allowLoadMore = isOpenLoadMore;
			showMoreData();
		}

		switch (status) {
			case STATUS_ON_REFRESH:
				base_swipe_refresh_widget.post(new Runnable() {
					@Override
					public void run() {
						base_swipe_refresh_widget.setRefreshing(false);
						status = STATUS_NORMAL;
					}
				});
				break;
			case STATUS_ON_LOAD_MORE:
				status = STATUS_NORMAL;
				if (allowLoadMore) {
					changeLoadMoreStatus(0);
				}
				break;
		}
	}

	public SwipeRefreshLayout getBase_swipe_refresh_widget() {
		return base_swipe_refresh_widget;
	}

	public ObservableListView getData_list() {
		return base_auto_load_data_list;
	}

	public int getRowCount() {
		return rowCount;
	}

	public void setRowCount(int rowCount) {
		this.rowCount = rowCount;
	}

	/**
	 * 允许加载更多
	 */
	public void allowLoadMore(boolean flag) {
		isOpenLoadMore = flag;
		allowLoadMore = flag;
		if (flag) {
			addFooterLayout();
		} else {
			if (footerLayout != null) {
				base_auto_load_data_list.removeFooterView(footerLayout);
				footerLayout = null;
			}
		}
	}

	/**
	 * 改变加载更多状态
	 */
	private void changeLoadMoreStatus(int status) {
		if (!isNeedShowFooter) {
			return;
		}
		addFooterLayout();
		switch (status) {
			case 0:
				if (jumpingBeans != null) {
					jumpingBeans.stopJumping();
				}
				tv_load_more.setText("加载更多");
				break;
			case 1:
				tv_load_more.setText("正在加载");
				jumpingBeans = JumpingBeans.with(tv_load_more).appendJumpingDots().build();
				break;
			case 2:
				if (jumpingBeans != null) {
					jumpingBeans.stopJumping();
				}
				tv_load_more.setText("无更多数据");
				break;
		}
	}

	private void doLoadMore() {
		if (status == STATUS_NORMAL && allowLoadMore) {
			if (onRefreshingListener != null) {
				status = STATUS_ON_LOAD_MORE;
				changeLoadMoreStatus(1);
				onRefreshingListener.onLoadMore();
			}
		}
	}

	/**
	 * 增加footer
	 */
	private void addFooterLayout() {
		if (footerLayout == null && isNeedShowFooter) {
			footerLayout = inflater.inflate(R.layout.base_auto_load_footer, null);
			base_auto_load_data_list.addFooterView(footerLayout);
			tv_load_more = footerLayout.findViewById(R.id.tv_load_more);
			footerLayout.setOnClickListener(new OnClickListener() {
				@Override
				public void onClick(View v) {
					doLoadMore();
				}
			});
		}
	}


	private OnRefreshingListener onRefreshingListener;

	public interface OnRefreshingListener {
		void onRefresh();

		void onLoadMore();
	}

	public void setOnRefreshingListener(OnRefreshingListener OnRefreshingListener) {
		this.onRefreshingListener = OnRefreshingListener;
	}

	@SuppressLint("HandlerLeak")
	private Handler refreshHandler = new Handler() {
		@Override
		public void handleMessage(Message msg) {
			onRefreshingListener.onRefresh();
		}
	};
}
